﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using PPI.Entities.Distribution;
using PPI.General;
using PPI.Mobile.Logic;
using VMind.Global;

namespace PPI.Client.Logic.Distribution
{
    public class LImage
    {
        public static void UpdatePoleImageUploadedData(EDistributionPoleImage image, Int32 poleId, string connectionString)
        {
            StringBuilder _query = new StringBuilder();
            _query.AppendLine("UPDATE tb_PoleImages set ImageServerID = " + image.ServerID + ", PoleServerID = " + poleId + ", UploadedDate = " +
                              DateTime.Now.ToString("yyyy-MM-dd") + " where ImageId =" + image.Id + " ");

            LMobileDatabase.ExecuteQuery(_query.ToString(), connectionString);
        }

        public static void UpdateMeterImageUploadedData(Int32 meterId, bool imageUploaded, string connectionString)
        {
            StringBuilder _query = new StringBuilder();
            _query.AppendLine("UPDATE tb_Meters set UploadedImage = " + (imageUploaded ? "1" : "0") + " where MeterId =" + meterId + " ");

            LMobileDatabase.ExecuteQuery(_query.ToString(), connectionString);
        }

        public static List<EDistributionPoleImage> GetPoleImages(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PI.ImageServerId, ");
            _sql.AppendLine("   PI.ImageId, ");
            _sql.AppendLine("   PI.FileName ");
            _sql.AppendLine("FROM tb_PoleImages PI ");
            _sql.AppendLine("WHERE PI.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<EDistributionPoleImage> lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                EDistributionPoleImage poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["ImageId"]);
                poleImage.ServerID = ConvertHelper.ToInt32(dr["ImageServerId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Pole;

                string _imageDirectory = DirHelper.GetImagesDirectory(connectionString);
                poleImage.FullName = _imageDirectory + Path.DirectorySeparatorChar + poleImage.FileName;
                
                lstImages.Add(poleImage);
            }

            return lstImages;
        }

        public static List<EDistributionPoleImage> GetTransformerImages(int idTransformer, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   idTransformerImage, ");
            _sql.AppendLine("   idTransformer, ");
            _sql.AppendLine("   FileName ");
            _sql.AppendLine("FROM tb_TransformerImages  ");
            _sql.AppendLine("WHERE idTransformer = " + idTransformer + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<EDistributionPoleImage> lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                EDistributionPoleImage poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["idTransformerImage"]);
                //poleImage.ServerID = ConvertHelper.ToInt32(dr["ImageServerId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Transformer;

                string _imageDirectory = DirHelper.GetImagesDirectory(connectionString);
                poleImage.FullName = _imageDirectory + Path.DirectorySeparatorChar + poleImage.FileName;

                lstImages.Add(poleImage);
            }

            return lstImages;
        }

        public static List<EDistributionPoleImage> GetSectionalizerImages(int idSectionalizer, string connectionString)
        {
            var _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   idSectionalizerImage, ");
            _sql.AppendLine("   idSectionalizer, ");
            _sql.AppendLine("   FileName ");
            _sql.AppendLine("FROM tb_SectionalizerImages  ");
            _sql.AppendLine("WHERE idSectionalizer = " + idSectionalizer + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];
            var lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                var poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["idSectionalizerImage"]);
                //poleImage.ServerID = ConvertHelper.ToInt32(dr["ImageServerId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Sectionalizer;

                string _imageDirectory = DirHelper.GetImagesDirectory(connectionString);
                poleImage.FullName = _imageDirectory + Path.DirectorySeparatorChar + poleImage.FileName;

                lstImages.Add(poleImage);
            }

            return lstImages;
        }

        public static List<EDistributionPoleImage> GetRecloserImages(int idRecloser, string connectionString)
        {
            var _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   idRecloserImage, ");
            _sql.AppendLine("   idRecloser, ");
            _sql.AppendLine("   FileName ");
            _sql.AppendLine("FROM tb_RecloserImages  ");
            _sql.AppendLine("WHERE idRecloser = " + idRecloser + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];
            var lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                var poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["idRecloserImage"]);
                //poleImage.ServerID = ConvertHelper.ToInt32(dr["ImageServerId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Recloser;

                string _imageDirectory = DirHelper.GetImagesDirectory(connectionString);
                poleImage.FullName = _imageDirectory + Path.DirectorySeparatorChar + poleImage.FileName;

                lstImages.Add(poleImage);
            }

            return lstImages;
        }

        public static List<EDistributionPoleImage> GetProjectPoleImages(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PI.ImageId, ");
            _sql.AppendLine("   PI.FileName ");
            _sql.AppendLine("FROM tb_PoleImages PI ");
            _sql.AppendLine("WHERE PI.PoleFk = " + distributionPole.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];
            List<EDistributionPoleImage> lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                EDistributionPoleImage poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["ImageId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Pole;

                lstImages.Add(poleImage);
            }

            return lstImages;
        }

        public static List<EDistributionPoleImage> GetProjectPoleImages(string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   PI.ImageId, ");
            _sql.AppendLine("   PI.FileName ");
            _sql.AppendLine("FROM tb_PoleImages PI; ");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];
            List<EDistributionPoleImage> lstImages = new List<EDistributionPoleImage>();

            foreach (DataRow dr in dt.Rows)
            {
                EDistributionPoleImage poleImage = new EDistributionPoleImage();

                poleImage.Id = ConvertHelper.ToInt32(dr["ImageId"]);
                poleImage.FileName = ConvertHelper.Tostring(dr["FileName"]);
                poleImage.ImageType = DistributionImageType.Pole;

                lstImages.Add(poleImage);
            }

            return lstImages;
        }
    }
}
